package com.twu.biblioteca;

public class Library
{
    static Book[] books;
    static Movie[] movies;
    static User[] users;

    Library()
    {
         initBooks();
         initMovies();
         initUsers();
    }

    static int initBooks()
    {
        books = new Book[5];
        books[0] =  new Book();books[0].bookNumber = 1111;books[0].title = "Head First Java";books[0].noOfCopies = 35;
        books[1] =  new Book();books[1].title = "Svn Tutorial";books[1].noOfCopies = 12;books[1].bookNumber = 2222;
        books[2] =  new Book();books[2].title = "How to solve it by computer";books[2].noOfCopies = 5;books[2].bookNumber =  3333;
        books[3] =  new Book();books[3].title = "Introduction to UNIX(2nd Edition)";books[3].noOfCopies = 0;books[3].bookNumber = 4444;
        books[4] =  new Book();books[4].title = "Principles of Programming Languages";books[4].noOfCopies = 44;books[4].bookNumber = 5555;
        return books.length;
    }

    static int initMovies()
    {
        movies = new Movie[5];
        movies[0] =  new Movie();movies[0].name = "Agneepath";movies[0].year = 2012;movies[0].director = "Karan Malhotra";movies[0].rating = 8;
        movies[1] =  new Movie();movies[1].name = "Bhaag Milkha Bhaag";movies[1].year = 2013;movies[1].director = "Rakesh Ompraksh Mehra";movies[1].rating = 10;
        movies[2] =  new Movie();movies[2].name = "Jindagi na Milegi Dobara";movies[2].year = 2012;movies[2].director = "Joya Akhtar";movies[2].rating = 9;
        movies[3] =  new Movie();movies[3].name = "Black";movies[3].year = 2005;movies[3].director = "Sanjay Leela Bhansali";movies[3].rating = 8;
        movies[4] =  new Movie();movies[4].name = "Barfi!";movies[4].year = 2013;movies[4].director = "Anurag Basu";movies[4].rating = 9;
        return movies.length;
    }

    static int initUsers()
    {
        users = new User[2];
        users[0] = new User();
        users[1] = new User();

        users[0].username = "ashwini";users[0].password = "ashwini@123";
        users[1].username = "mihir";users[1].password = "mihir#yahoo";
        users[0].booksReserved.add(1111);      //for testing the reserveBook() method...to be removed in the end

        users[0].userMembershipDetails.put("name","Ashwini");users[0].userMembershipDetails.put("email-id","ashwinidesai2310@gmail.com");users[0].userMembershipDetails.put("phoneNo", "9876543210");
        users[1].userMembershipDetails.put("name","Mihir");users[1].userMembershipDetails.put("email-id","mihir.deshpande@gmail.com");users[1].userMembershipDetails.put("phoneNo","9231456446");
        return  users.length;
    }

    static int listAllBooks()
    {
        System.out.println("Book Number"+" "+"Book Title");
        for(int i = 0; i < books.length; i++)
            System.out.println(books[i].bookNumber+"        "+books[i].title);
        return books.length;
    }

    static int showAllMovies()
    {
          System.out.println("Movie"+"                      "+"Year"+"  "+"Director"+"                      "+"Rating");
          for(int i = 0; i < movies.length; i++)
              System.out.println(movies[i].name+"                        "+movies[i].year+"   "+movies[i].director+"                       "+movies[i].rating);
          return movies.length;
    }

    private User getCurrentUserObject(String currentUser)
    {
        for(int i = 0; i < users.length; i++)
            if(users[i].username.equals(currentUser))
                return users[i];
        return null;
    }

    boolean reserveBook(int bookNumber, String currentUser, boolean isUserLoggedIN)
    {
        if(isUserLoggedIN)
        {
            if(getCurrentUserObject("ashwini").isBookAlreadyReserved(bookNumber))
            {
                for(int i = 0;i < books.length; i++)
                {
                    if(books[i].bookNumber == bookNumber)
                    {
                        if(books[i].isBookAvailable())
                        {
                            System.out.println("Thank You! Enjoy the book.");
                            return true;
                        }
                    }
                }
                System.out.println("Sorry we don't have that book yet.");
            }
            else
                System.out.println("You have already booked the book");
        }
        else
            System.out.println("Please log in first!");
        return false;
    }

    boolean getUserMembershipDetails(String username,boolean isUserLoggedIN)
    {
        if(isUserLoggedIN)
        {
            for(User user : Library.users)
            {
                if(user.username.equals(username))
                    System.out.println(user.userMembershipDetails.get("name")+" "+user.userMembershipDetails.get("email-id")+" "+user.userMembershipDetails.get("phoneNo"));
            }
            return true;
        }
        System.out.println("Please talk to a librarian. Thank you");
        return false;
    }


}
